package com.yq.day27;

import org.junit.Test;

import javax.xml.transform.Source;
import java.nio.ByteBuffer;

/**
 * @author 3307235355@qq.com
 * @since 2022/06/27 21:25
 */
public class DEmo02 {
    @Test
    public void myTest() {
        ByteBuffer buffer = ByteBuffer.allocate(10);

        System.out.println(buffer);
    }

    //向缓冲区写数据
    @Test
    public void myTest01() {
        ByteBuffer buffer = ByteBuffer.allocate(20);
        System.out.println(buffer);
        buffer.put((byte) 97);
        System.out.println(buffer);

        //    标记mark
        buffer.mark();


        buffer.put("abcd".getBytes());
        System.out.println(buffer);

        // buffer.reset();


        //切换位置到0，从头开始
        buffer.flip();
        System.out.println(buffer);

    }

    //    循环读取
    @Test
    public void myTest02() {
        ByteBuffer buffer = ByteBuffer.allocate(20);


        buffer.put("abcd".getBytes());
        System.out.println(buffer);
        buffer.flip();

        // boolean hasRemaining()
        // 告知在当前位置和限制之间是否有元素(position到limit区间内)。
        // 当且仅当此缓冲区中至少还有一个元素时返回 true
        while (buffer.hasRemaining()) {
            byte b = buffer.get();
            System.out.println(b);
        }


    }


}
